#ifndef WWFRONTEND_UTILITY_H
#define WWFRONTEND_UTILITY_H

#include "../Biz/WWCommonUtility.h"
#include <stdio.h>
#include <string>
#include <vector>
#include <termios.h>
#include <unistd.h>

using namespace wwcommon;

#define ECHOFLAGS (ECHO|ECHOE|ECHOK|ECHONL)
class FrontEndUtility
{
public:
    static int set_noecho(bool noecho)
    {
        struct termios term;
        if(tcgetattr(STDIN_FILENO, &term) == -1)
        {
            perror("get stdin attr failed.");
            return -1;
        }
        if(noecho)
        {
            term.c_lflag &= ~ECHOFLAGS;
        }
        else
        {
            term.c_lflag |= ECHOFLAGS;
        }
        if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &term) != 0)
        {
            perror("set attr for stdin failed.");
            return -1;
        }
        return 0;
    }
    static void split_string(std::string src, std::vector<std::string>& deststrs, const std::string& splitter)
    {
        CommonUtility::TrimStr(src, 3, splitter);
        if(src.empty())
            return;
        size_t spos = 0;
        size_t epos = src.find(splitter);
        while(epos != std::string::npos)
        {
            std::string tmpstr = src.substr(spos, epos - spos);
            CommonUtility::TrimStr(tmpstr, 3, splitter);
            if(tmpstr != "")
                deststrs.push_back(tmpstr);
            spos = epos + 1;
            epos = src.find(splitter, spos);
        }
        deststrs.push_back(src.substr(spos));
    }
};
#endif
